package com.baizhi.cmfz.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @RequestMapping("loginAdmin")
    @LogAnnotation(value = "管理员登录")
    public String login(Admin admin, Map<String, String> map, String code, HttpSession session) {
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        if (!((ICaptcha) subject.getSession().getAttribute("captcha")).verify(code.toLowerCase())) {
            map.put("msg", "验证码输入错误");
            return "login";
        }
        //封装Token
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(),
                admin.getPassword());

        //调用登录方法
        try {
            subject.login(token);
            return "redirect:/main.jsp";
        } catch (AuthenticationException e) {
            map.put("msg", "用户名不存在或密码错误");
            return "login";
        }
    }

    @RequestMapping("getCaptcha")
    public void captcha(HttpServletResponse response, HttpSession session) {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        ICaptcha captcha = CaptchaUtil.createCircleCaptcha(250, 100, 4, 15);
        try (OutputStream outputStream = response.getOutputStream()) {
            SecurityUtils.getSubject().getSession().setAttribute("captcha", captcha);
            captcha.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //添加管理员
    @RequestMapping("add")
    @ResponseBody
    @LogAnnotation(value = "添加管理员", type = "insert")
    public boolean addAdmin(Admin admin) {
        return adminService.addAdmin(admin) > 0;
    }

    //删除管理员
    @RequestMapping("remove")
    @ResponseBody
    @LogAnnotation(value = "删除管理员", type = "delete")
    public boolean removeAdminById(int[] ids) {
        return adminService.removeAdminById(ids) > 0;
    }

    //修改管理员
    @RequestMapping("update")
    @ResponseBody
    @LogAnnotation(value = "修改管理员信息", type = "update")
    public boolean updateAdmin(Admin admin) {
        return adminService.updateAdmin(admin) > 0;
    }

    //根据id查找管理员
    @RequestMapping("selectOne")
    @ResponseBody
    @LogAnnotation(value = "根据id查找管理员")
    public Admin selectOneAdmin(int id) {
        return adminService.selectOneAdmin(id);
    }

    //分页展示所有管理员
    @RequestMapping("all")
    @ResponseBody
    @LogAnnotation(value = "查找所有管理员")
    public Map selectAllAdmin(int page, int limit) {
        return adminService.selectAllAdmin(page, limit);
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "login";
    }
}
